﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using DimensionData;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using DimensionData.Data2;
using System.Diagnostics;

namespace DimensionAddIn
{
    public partial class Ribbon2
    {
        private void Ribbon2_Load(object sender, RibbonUIEventArgs e)
        {

        }

        /// <summary>
        /// 测试-地产-页面维度不显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button16_Click(object sender, RibbonControlEventArgs e)
        {
            Windows.BusyIndicatorForm busyForm = new Windows.BusyIndicatorForm();

            busyForm.ShowMask(() =>
            {
                Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;
                if (DocAddIn.Instances.ContainsKey(sheet.Name))
                {
                    DocAddIn.Instances.Remove(sheet.Name);
                }

                sheet.UsedRange.Delete();

                DTable table0 = new DTable() { Code = "DC.PROJ", Name = "项目" };
                DTable table1 = new DTable() { Code = "DC.KM", Name = "科目" };
                DTable table2 = new DTable() { Code = "DC.QJ", Name = "期间" };
                DTable table3 = new DTable() { Code = "DC.CBI", Name = "成本项" };

                DCTable yTable = table0.Combine(table1);
                DCTable xTable = table2.Combine(table3);

                DContext context = new DContext();
                context.DocCode = "DC";

                DTree yTree = yTable.CalcDTree(context);
                DTree xTree = xTable.CalcDTree(context);

                List<DRow> yDT = yTree.Complanate2();
                List<DRow> xDT = xTree.Complanate2();

                List<PageDim> pageDTable = new List<PageDim>();
                pageDTable.Add(new PageDim() { DimData = table0.GetData(context), Direction = DimDirection.Y, Index = 0 });

                List<FormulaRule> fRules = DMockData.GetFormulaRules(context.DocCode);
                DocAddIn addin = new DocAddIn(sheet.Name);
                addin.Init(yTree, xTree, pageDTable, fRules);
                addin.Schema.DocType = context.DocCode;

                DocRender render = new DocRender(sheet);

                // 设置初始的页面维度选择数据
                for (int pdtIndex = 0; pdtIndex < pageDTable.Count; pdtIndex++)
                {
                    pageDTable[pdtIndex].SelectedNames = DocAddIn.GetAllNames(
                        pageDTable[pdtIndex].DimData,
                        pageDTable[pdtIndex].DimData.Nodes[0].Name);
                }

                render.RenderContent(addin.Schema, addin.Matrix);
                bool isNewSheet = new DocumentRender(sheet).RenderDims(sheet.Application.ActiveWorkbook, pageDTable);
                //new DocumentRender(sheet).RenderPageDim(sheet, pageDTable);
                render.RenderPageDim(addin.Schema, addin.Matrix);

                DocAddIn.Instances.Add(sheet.Name, addin);
                addin.YDTree = yTree;
                addin.XDTree = xTree;
                addin.PageDims = pageDTable;

                if (isNewSheet)
                {
                    sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);
                    //sheet.Change += new Excel.DocEvents_ChangeEventHandler(worksheet_Change);
                    Globals.ThisAddIn.Application.SheetChange += new Excel.AppEvents_SheetChangeEventHandler(Application_SheetChange);
                }
            });
        }

        /// <summary>
        /// 测试-地产-页面维度显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Windows.BusyIndicatorForm busyForm = new Windows.BusyIndicatorForm();

            busyForm.ShowMask(() =>
            {
                Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;

                if (DocAddIn.Instances.ContainsKey(sheet.Name))
                {
                    DocAddIn.Instances.Remove(sheet.Name);
                }

                sheet.UsedRange.Delete();

                DTable table0 = new DTable() { Code = "DC.PROJ", Name = "项目" };
                DTable table1 = new DTable() { Code = "DC.KM", Name = "科目" };
                DTable table2 = new DTable() { Code = "DC.QJ", Name = "期间" };
                DTable table3 = new DTable() { Code = "DC.CBI", Name = "成本项" };

                DCTable yTable = table0.Combine(table1);
                DCTable xTable = table2.Combine(table3);

                DContext context = new DContext();
                context.DocCode = "DC";

                DTree yTree = yTable.CalcDTree(context);
                DTree xTree = xTable.CalcDTree(context);

                List<DRow> yDT = yTree.Complanate2();
                List<DRow> xDT = xTree.Complanate2();

                List<PageDim> pageDTable = new List<PageDim>();
                pageDTable.Add(new PageDim() { DimData = table0.GetData(context), Direction = DimDirection.Y, Index = 0, IsVisible = true });
                //pageDTable.Add(new PageDim() { DimData = table1.GetData(context), Direction = DimDirection.Y, Index = 1, IsVisible = true });

                List<FormulaRule> fRules = DMockData.GetFormulaRules(context.DocCode);
                DocAddIn addin = new DocAddIn(sheet.Name);
                addin.Init(yTree, xTree, pageDTable, fRules);
                addin.Schema.DocType = context.DocCode;

                DocRender render = new DocRender(sheet);

                // 设置初始的页面维度选择数据
                for (int pdtIndex = 0; pdtIndex < pageDTable.Count; pdtIndex++)
                {
                    pageDTable[pdtIndex].SelectedNames = DocAddIn.GetAllNames(
                        pageDTable[pdtIndex].DimData,
                        pageDTable[pdtIndex].DimData.Nodes[0].Name);
                }

                render.RenderContent(addin.Schema, addin.Matrix);
                bool isNewSheet = new DocumentRender(sheet).RenderDims(sheet.Application.ActiveWorkbook, pageDTable);
                //new DocumentRender(sheet).RenderPageDim(sheet, pageDTable);
                render.RenderPageDim(addin.Schema, addin.Matrix);

                DocAddIn.Instances.Add(sheet.Name, addin);
                addin.YDTree = yTree;
                addin.XDTree = xTree;
                addin.PageDims = pageDTable;

                if (isNewSheet)
                {
                    sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);
                    //sheet.Change += new Excel.DocEvents_ChangeEventHandler(worksheet_Change);
                    Globals.ThisAddIn.Application.SheetChange += new Excel.AppEvents_SheetChangeEventHandler(Application_SheetChange);
                }
            });
        }


        void Application_SheetChange(object Sh, Excel.Range Target)
        {
            try
            {
                Debug.WriteLine("worksheet range value change");
                if (DocAddIn.Instances.ContainsKey(Target.Worksheet.Name))
                {
                    DocAddIn addin = DocAddIn.Instances[Target.Worksheet.Name];
                    if (!addin.IsChangedBySelf)
                    {
                        addin.OnCellValueChanged(Target);
                        //Target.Worksheet.Change += new Excel.DocEvents_ChangeEventHandler(worksheet_Change);
                    }
                }
            }
            catch
            {
            }
        }

        void sheet_SelectionChange(Excel.Range Target)
        {
            try
            {
                if (DocAddIn.Instances.ContainsKey(Target.Worksheet.Name))
                {
                    DocAddIn addin = DocAddIn.Instances[Target.Worksheet.Name];
                    //addin.OnCellValueChanged(Target);

                    int targetCellRow = Target.Row;
                    int targetCellCol = Target.Column;
                    if (addin.Matrix.VisualRect.DataBeginRow <= targetCellRow
                        && addin.Matrix.VisualRect.DataBeginCol <= targetCellCol
                        && addin.Matrix.VisualRect.DataEndRow >= targetCellRow
                        && addin.Matrix.VisualRect.DataEndCol >= targetCellCol)
                    {
                        // 获取完整的维度路径

                        List<DRow> yRows = addin.Schema.YTree.Complanate2();
                        List<DRow> xRows = addin.Schema.XTree.Complanate2();
                        Excel.Range selRange = Target;

                        int deltaRow = targetCellRow - addin.Matrix.VisualRect.DataBeginRow;
                        int deltaCol = targetCellCol - addin.Matrix.VisualRect.DataBeginCol;

                        // 获取ydimrowindex, xdimrowindex
                        DRow yRow = yRows[addin.Matrix.VisualRect.YRowIndexList[deltaRow]];
                        DRow xRow = xRows[addin.Matrix.VisualRect.XRowIndexList[deltaCol]];
                        string codePath = string.Format("{0}{1}", yRow.GetCodePath(), xRow.GetCodePath());
                        label1.Label = codePath;
                        label2.Label = yRow.GetCodePath();
                        label3.Label = xRow.GetCodePath();
                    }
                }
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {
            Windows.BusyIndicatorForm busyForm = new Windows.BusyIndicatorForm();

            busyForm.ShowMask(() =>
            {
                Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;

                if (DocAddIn.Instances.ContainsKey(sheet.Name))
                {
                    DocAddIn.Instances.Remove(sheet.Name);
                }

                sheet.UsedRange.Delete();

                DTable table0 = new DTable() { Code = "JR.BIZ", Name = "业务类型" };
                DTable table1 = new DTable() { Code = "JR.KM", Name = "科目" };
                DTable table2 = new DTable() { Code = "JR.QJ", Name = "期间" };

                DCTable yTable = table0.Combine(table1);
                DCTable xTable = table2.Combine();

                DContext context = new DContext();
                context.DocCode = "JR";

                DTree yTree = yTable.CalcDTree(context);
                DTree xTree = xTable.CalcDTree(context);

                List<DRow> yDT = yTree.Complanate2();
                List<DRow> xDT = xTree.Complanate2();

                List<PageDim> pageDTable = new List<PageDim>();
                pageDTable.Add(new PageDim() { DimData = table0.GetData(context), Direction = DimDirection.Y, Index = 0, IsVisible = true });

                List<FormulaRule> fRules = DMockData.GetFormulaRules(context.DocCode);
                List<DataRefRule> rRules = DMockData.GetDataRefs(context.DocCode);
                
                DocAddIn addin = new DocAddIn(sheet.Name);
                addin.Init(yTree, xTree, pageDTable, fRules, rRules);
                addin.Schema.DocType = context.DocCode;

                DocRender render = new DocRender(sheet);

                // 设置初始的页面维度选择数据
                for (int pdtIndex = 0; pdtIndex < pageDTable.Count; pdtIndex++)
                {
                    pageDTable[pdtIndex].SelectedNames = DocAddIn.GetAllNames(
                        pageDTable[pdtIndex].DimData,
                        pageDTable[pdtIndex].DimData.Nodes[0].Name);
                }

                render.RenderContent(addin.Schema, addin.Matrix);
                bool isNewSheet = new DocumentRender(sheet).RenderDims(sheet.Application.ActiveWorkbook, pageDTable);
                //new DocumentRender(sheet).RenderPageDim(sheet, pageDTable);
                render.RenderPageDim(addin.Schema, addin.Matrix);

                DocAddIn.Instances.Add(sheet.Name, addin);
                addin.YDTree = yTree;
                addin.XDTree = xTree;
                addin.PageDims = pageDTable;

                if (isNewSheet)
                {
                    sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);
                    Globals.ThisAddIn.Application.SheetChange += new Excel.AppEvents_SheetChangeEventHandler(Application_SheetChange);
                }
            });
        }

        private void button3_Click(object sender, RibbonControlEventArgs e)
        {
            Windows.BusyIndicatorForm busyForm = new Windows.BusyIndicatorForm();

            busyForm.ShowMask(() =>
            {
                Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;

                if (DocAddIn.Instances.ContainsKey(sheet.Name))
                {
                    DocAddIn.Instances.Remove(sheet.Name);
                }

                sheet.UsedRange.Delete();

                DTable table0 = new DTable() { Code = "JR.BIZ", Name = "业务类型" };
                DTable table1 = new DTable() { Code = "JR.KM", Name = "科目" };
                DTable table2 = new DTable() { Code = "JR.QJ", Name = "期间" };

                DCTable yTable = table0.Combine(table1);
                DCTable xTable = table2.Combine();

                DContext context = new DContext();
                context.DocCode = "JRM";

                DTree yTree = yTable.CalcDTree(context);
                DTree xTree = xTable.CalcDTree(context);

                List<DRow> yDT = yTree.Complanate2();
                List<DRow> xDT = xTree.Complanate2();

                List<PageDim> pageDTable = new List<PageDim>();
                pageDTable.Add(new PageDim() { DimData = table0.GetData(context), Direction = DimDirection.Y, Index = 0, IsVisible = true });
                pageDTable.Add(new PageDim() { DimData = table2.GetData(context), Direction = DimDirection.X, Index = 0, IsVisible = true });

                List<FormulaRule> fRules = DMockData.GetFormulaRules(context.DocCode);
                List<DataRefRule> rRules = DMockData.GetDataRefs(context.DocCode);

                DocAddIn addin = new DocAddIn(sheet.Name);
                addin.Init(yTree, xTree, pageDTable, fRules, rRules);
                addin.Schema.DocType = context.DocCode;

                DocRender render = new DocRender(sheet);

                // 设置初始的页面维度选择数据
                for (int pdtIndex = 0; pdtIndex < pageDTable.Count; pdtIndex++)
                {
                    pageDTable[pdtIndex].SelectedNames = DocAddIn.GetAllNames(
                        pageDTable[pdtIndex].DimData,
                        pageDTable[pdtIndex].DimData.Nodes[0].Name);
                }

                render.RenderContent(addin.Schema, addin.Matrix);
                bool isNewSheet = new DocumentRender(sheet).RenderDims(sheet.Application.ActiveWorkbook, pageDTable);
                //new DocumentRender(sheet).RenderPageDim(sheet, pageDTable);
                render.RenderPageDim(addin.Schema, addin.Matrix);

                DocAddIn.Instances.Add(sheet.Name, addin);
                addin.YDTree = yTree;
                addin.XDTree = xTree;
                addin.PageDims = pageDTable;

                if (isNewSheet)
                {
                    sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);
                    Globals.ThisAddIn.Application.SheetChange += new Excel.AppEvents_SheetChangeEventHandler(Application_SheetChange);
                }
            });
        }

        private void button4_Click(object sender, RibbonControlEventArgs e)
        {
            Windows.BusyIndicatorForm busyForm = new Windows.BusyIndicatorForm();

            busyForm.ShowMask(() =>
            {
                Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;

                if (DocAddIn.Instances.ContainsKey(sheet.Name))
                {
                    DocAddIn.Instances.Remove(sheet.Name);
                }

                sheet.UsedRange.Delete();

                DTable table0 = new DTable() { Code = "ORG", Name = "组织机构" };
                DTable table1 = new DTable() { Code = "JR.ZJCB.BIZ", Name = "业务类型" };
                DTable table2 = new DTable() { Code = "JR.ZJCB.I", Name = "项" };

                DCTable yTable = table0.Combine(table1);
                DCTable xTable = table2.Combine();

                DContext context = new DContext();
                context.DocCode = "JR.ZJCB";

                DTree yTree = yTable.CalcDTree(context);
                DTree xTree = xTable.CalcDTree(context);

                List<DRow> yDT = yTree.Complanate2();
                List<DRow> xDT = xTree.Complanate2();

                List<PageDim> pageDTable = new List<PageDim>();
                pageDTable.Add(new PageDim() { DimData = table0.GetData(context), Direction = DimDirection.Y, Index = 0, IsVisible = false });
                //pageDTable.Add(new PageDim() { DimData = table2.GetData(context), Direction = DimDirection.X, Index = 0, IsVisible = true });

                List<FormulaRule> fRules = DMockData.GetFormulaRules(context.DocCode);
                List<DataRefRule> rRules = DMockData.GetDataRefs(context.DocCode);

                DocAddIn addin = new DocAddIn(sheet.Name);
                addin.Init(yTree, xTree, pageDTable, fRules, rRules);
                addin.Schema.DocType = context.DocCode;

                DocRender render = new DocRender(sheet);

                // 设置初始的页面维度选择数据
                for (int pdtIndex = 0; pdtIndex < pageDTable.Count; pdtIndex++)
                {
                    pageDTable[pdtIndex].SelectedNames = DocAddIn.GetAllNames(
                        pageDTable[pdtIndex].DimData,
                        pageDTable[pdtIndex].DimData.Nodes[0].Name);
                }

                render.RenderContent(addin.Schema, addin.Matrix);
                bool isNewSheet = new DocumentRender(sheet).RenderDims(sheet.Application.ActiveWorkbook, pageDTable);
                //new DocumentRender(sheet).RenderPageDim(sheet, pageDTable);
                render.RenderPageDim(addin.Schema, addin.Matrix);

                DocAddIn.Instances.Add(sheet.Name, addin);
                addin.YDTree = yTree;
                addin.XDTree = xTree;
                addin.PageDims = pageDTable;

                if (isNewSheet)
                {
                    sheet.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);
                    Globals.ThisAddIn.Application.SheetChange += new Excel.AppEvents_SheetChangeEventHandler(Application_SheetChange);
                }
            });
        }
    }
}
